package com.lancoo.answer.view.fragment.paragraphSort

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import com.lancoo.answer.R
import com.lancoo.answer.component.InteractWebView
import com.lancoo.answer.databinding.EvViewParagraphSortLookingBinding
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.Ques
import com.lancoo.answer.view.base.BaseVMFragment
import com.lancoo.answer.widget.combinationView.AnswerAnalysisVPView

/**
 * Created by Dadong on 2021/12/27
 * Describe: 段落排序，多项对应查看
 */
class ParagraphSortingChildLookingFragment : BaseVMFragment<EvViewParagraphSortLookingBinding>() {

    private var typeIndex = 0
    private var quesIndex = 0
    private var childIndex = 0
    private var defaultLoadIndex = 0
    private lateinit var ques: Ques
    private var itemSortIndex: String = "" //当前选择的序号

    companion object {
        fun loadFragment(
            typeIndex: Int,
            quesIndex: Int,
            childIndex: Int,
            defaultLoadIndex: Int
        ): Fragment? {
            val mBundle = Bundle()
            mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mBundle.putInt(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mBundle.putInt(AnswerConstant.KEY_CHILDINDEX, childIndex)
            mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX, defaultLoadIndex)
            val paragraphSortingChildLookingFragment = ParagraphSortingChildLookingFragment()
            paragraphSortingChildLookingFragment.arguments = mBundle
            return paragraphSortingChildLookingFragment
        }
    }

    override fun initData() {
        if (ConstantBean.constantExamBean == null) {
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = arguments!!.getInt(AnswerConstant.KEY_TYPEINDEX)
        quesIndex = arguments!!.getInt(AnswerConstant.KEY_QUESINDEX)
        childIndex = arguments!!.getInt(AnswerConstant.KEY_CHILDINDEX)
        var loadChildIndex = arguments!!.getInt(AnswerConstant.KEY_LOADCHILDINDEX)
        Log.e("填空题", "loadChildIndex:" + loadChildIndex)
        ques = kindBeanList!![typeIndex].QuesList!![quesIndex]
        if (loadChildIndex < 0) {
            itemSortIndex = ques.ChildList!![0].ItemList!![0].ItemSortIndex!!
        } else {
            itemSortIndex = ques.ChildList!![0].ItemList!![loadChildIndex].ItemSortIndex!!
        }
        setInteractWebView()
    }


    override fun initView(view: View) {
    }

    override fun getLayoutId(): Int {
        return R.layout.ev_view_paragraph_sort_looking
    }

    //加载题干
    private fun setInteractWebView() {
        val askBody = ques.ChildList!![childIndex].ChildAsk
        mBinding.interactWv.initWebView(
            activity!!,
            askBody!!,
            object : InteractWebView.Callback {
                override fun finishCallback() {
                    mBinding.interactWv.showOrHideAnalysisButton(1)
                    wvLoadUrl(itemSortIndex)
                    mBinding.answerAnalysisView.setItemList(
                        ques.ChildList!![0].ItemList,
                        getRealIndex()
                    )
                }

                override fun clickCallback(position: String) {
                    itemSortIndex = position
                    wvLoadUrl(itemSortIndex)
                    mBinding.answerAnalysisView.setItemList(
                        ques.ChildList!![0].ItemList,
                        getRealIndex()
                    )
                }
            }
        )

        mBinding.answerAnalysisView.setOnAnswerAnalysisPageChangeCallback(object :
            AnswerAnalysisVPView.OnAnswerAnalysisPageChangeCallback() {
            override fun onPageSelect(position: Int) {
                super.onPageSelect(position)
                itemSortIndex = ques.ChildList!![0].ItemList!![position].ItemSortIndex!!
                wvLoadUrl(itemSortIndex)
            }
        })
        mBinding.interactWv.setDisableInterceptTouch(true)
    }

    //获取Item
    fun getRealIndex(): Int {
        ques.ChildList!![0].ItemList!!.forEachIndexed { index, item ->
            if (item.ItemSortIndex == itemSortIndex) {
                return index
            }
        }
        return 0
    }


    /**
     *   答题点下划线，高亮等等绘制
     *   词汇匹配'm'需要展示标准答案
     */
    private fun wvLoadUrl(position: String = "-1") {
        mBinding.interactWv.scrollPosition(position)
        ques.ChildList!![0].ItemList!!.forEachIndexed { index, item ->
            mBinding.interactWv.loadUrl(
                item,
                position == item.ItemSortIndex.toString(),
                isAnswer = false,
                true
            )
        }
    }

    fun adjustFontSize(){
        mBinding.interactWv.setFontSize()
    }
}